<?php namespace web\home\controller;


class Shoplist extends Conf_common{
	
//	产品列表
	public function index(){
//		获取请求分类
		$cid=Q('get.cid',0,'intval');
//		查询当前分类下的所有子级分类ID
//		1.获取所有分类id
		$cidData=Db::table('category')->get();
		$cids=$this->getSon($cid,$cidData);
		$cids[]=$cid;
//		p($cids);die;
//		获取所有分类下的商品
		$gidData=Db::table('goods')->where("cid in(".implode(',',$cids).")")->lists('gid');
//		p($gidData);die;
//		获取商品属性规格和所属分类
		$attrData=Db::table('goods_attr')->where("gid in(".implode(',',$gidData).")")->groupBy('gtvalue')->get();
//		p($attrData);die;
			foreach($attrData as $k=>$v){
				$attrData[$k]['cid']=Db::table('goods')->where('gid',$v['gid'])->pluck('cid');
			}
			$Attrs=array();
			foreach($attrData as $k=>$v){
				$attrData[$k]['cid']=Db::table('goods')->where('gid',$v['gid'])->pluck('cid');
				$Attrs[$v['taid']][]=$v;
				
			}
//			p($Attrs);die;
			foreach($Attrs as $k=>$v){
				foreach($v as $kk=>$vv){
					$AttrData[]=array(
						'cid'=>$vv['cid'],
						'cname'=>Db::table('category')->where('cid',$vv['cid'])->pluck('cname'),
						'taname'=>Db::table('type_attr')->where('taid',$k)->pluck('taname'),
						'tavalue'=>$v,
					);
				}
			}
//			p($AttrData);
//			合并相同分类的数组
			foreach($AttrData as $k=>$v){
				
				$Attrdata[$v['taname']]=$v;
			}

			p($Attrdata);die;
//			分配数据到页面
			View::with('AttrData',$AttrData);
		
		
	}
	
//	递归
	public function getSon($cid,$cidData){
		$temp=array();
			foreach ($cidData as $k => $v) {
				if($v['pid']==$cid){
					$temp[]=$v['cid'];
					$temp=array_merge($temp,$this->getSon($v['cid'],$cidData));
				}
			}
			return $temp;
	}
	
	
}

?>